package com.hsq.androidproject.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;

import com.example.lenovo.androidproject3.R;


/**
 * Created by lenovo on 2017/1/31.
 */

public class SelfDialog extends Dialog {

    private Button yes;//确定按钮  
    private Button no;//取消按钮  
    //确定文本和取消文本的显示内容  
    private String yesStr, noStr;
    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器  
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器  

    /**
     *     * 设置取消按钮的显示内容和监听 
     *
     * @param str    @param onNoOnclickListener    
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     *   * 设置确定按钮的显示内容和监听 
     *  @param str 
     *  * @param onYesOnclickListener      
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener)
    {
        if (str != null) {
            yesStr = str;
        }
        this.yesOnclickListener = onYesOnclickListener;

    }

    public SelfDialog(Context context) {
        super(context, R.style.MyDialog);
    }
    private static int getStatusBarHeight(Context context) {
        int statusBarHeight = 0;
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = res.getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.setStatusBarColor(Color.BLACK);   //这里动态修改颜色
        }
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ViewGroup decorViewGroup = (ViewGroup) window.getDecorView();
        View statusBarView = new View(window.getContext());
        int statusBarHeight = getStatusBarHeight(window.getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, statusBarHeight);
        params.gravity = Gravity.TOP;
        statusBarView.setLayoutParams(params);
        statusBarView.setBackgroundColor(Color.parseColor("#000000"));
        decorViewGroup.addView(statusBarView);
        setContentView(R.layout.dialog_layout);
        //按空白处不能取消动画  
        //setCanceledOnTouchOutside(false);
        //初始化界面控件  
        initView(); //初始化界面数据  
        initEvent();
    }

    /**
     * 初始化界面的确定和取消监听器 *      
     */
    private void initEvent() { //设置确定按钮被点击后，向外界提供监听  
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick();
                }
            }
        });//设置取消按钮被点击后，向外界提供监听  
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }


    /**
     *  * 初始化界面控件 
     */
    private void initView() {
        yes = (Button) findViewById(R.id.yes);
        no = (Button) findViewById(R.id.no);

    }

    /**
     *  * 设置确定按钮和取消被点击的接口    
     */

    public interface onYesOnclickListener {

        void onYesClick();
    }

    public interface onNoOnclickListener {
        void onNoClick();
    }
}
